<?php
/**
	Classes:
	item (myDbTb :: myClass)
	item :: small_catalog_item
	item :: large_catalog_item
*/

/*
 * This class defines a generic item. It will serve as a template for definining the other concret classes that are:
 * - small_catalog_item
 * - large_catalog_item
 * - order_item
 */
abstract class item extends myDbTb{
	public $id;
	public $name;
	public $price;
	public $sort_order;

	/*
	 * It loads the product data from the database
	 */
	function db_load() {
		$sql = "SELECT p.* FROM " .
			TABLE_PRODUCTS . " p" .
			" where p.status=1" .
			" AND p.id = " . $this->id .
			" LIMIT 1";

		return $this->sql_request($sql);
	}
}
/*
 * This class is oriented for catalog items printing needs.
 */
class small_catalog_item extends item{
	public $description;
	public $image;

	function db_load() {
		parent::db_load();
		global $server;
		$this->image=$server->my_site[0]->path.$server->my_site[0]->folder."catalog/images/small/".$this->image;
		if (preg_match('/<p>.*?<\/p>/si', $this->description, $matches)) {
			$this->description=$matches[0];
		}
	}
}

/*
 *  This class is oriented to for catalog items printing at large size version.
 */
class large_catalog_item extends item{
	public $description;
	public $image;
	public $next_id;
	public $before_id;

	//This functions set the arrows items id of the next and before producct item
	function set_next_before() {
		global $server;
		//First we take every product id
		$sql = "select distinct p.id from " .
			TABLE_PRODUCTS . " p left join " .
			TABLE_CATEGORIES . " c on p.category_id=c.id" .
			" and p.status=1" .
			" order by p.category_id, c.sort_order, p.sort_order";
		$result = $server->db->link->query($sql);
		if ($server->db->link->affected_rows) {
			$ids=array();
			while (list($result_id) = $result->fetch_row()) {
				array_push($ids,$result_id);
			}
			/* We will search for the backward and forward id */
			$indice=0;
			for ($i=0;$i<count($ids);$i++) {
				if ($ids[$i]==$this->id) {
					$indice=$i;
					break;
				}
			}
			if ($indice > 0) {
				$this->before_id=$ids[$indice-1];
			}
			else {
				$this->before_id=$ids[count($ids)-2];	//When we are at the firs item the before item is the last one
			}
			if ($indice < count($ids) - 1) {
				$this->next_id=$ids[$indice+1];
			}
			else {
				$this->next_id=$ids[0]; //At las item the next one will be the first one
			}
		 }
	}
	function db_load() {
		parent::db_load();
		global $server;
		$this->image=$server->my_site[0]->path.$server->my_site[0]->folder."catalog/images/large/".$this->image;
	}
}
/*
 * This item porpouse is serve for printing the order items
 */
class order_item extends item{
	public $order_id;
	public $quantity;
	public $item_id;

	/*
	 * It loads the product data from the database
	 */
	function db_load() {
		$sql = "SELECT p.name, p.quantity FROM " .
			TABLE_ORDERS_PRODUCTS . " op" .
			" where op.item_id = " . $this->id .
			" and op.order_id = " . $this->order_id .
			" LIMIT 1";

		return $this->sql_request($sql);
	}

	function db_insert() {
		$sql = "INSERT INTO "
				. TABLE_ORDERS_PRODUCTS
			. " ("
				. " order_id"
				. " , item_id"
				. " , name"
				. " , price"
				. " , quantity"
			. " ) VALUES ("
				. $this->order_id
				. " , " . $this->id
				. " , '" . $this->name ."'"
				. " , '" . $this->price ."'"
				. " , '" . $this->quantity ."'"
			. " )";

		return $this->sql_request($sql);
	}
}
class admin_item extends item{
	public $description;
	public $image;
	public $category_id;
	public $status;

	function db_load() {
		parent::db_load();
		global $server;
		$this->image=$server->my_site[0]->path.$server->my_site[0]->folder."catalog/images/small/".$this->image;
	}
	function db_delete() {
		$sql="DELETE FROM ".
		TABLE_PRODUCTS .
		" WHERE id=" . $this->id .
		" LIMIT 1";
		return $this->sql_request($sql);
	}
	function db_insert() {
		$sql = "INSERT INTO "
				. TABLE_PRODUCTS
			. " ("
				. " name"
				. " , category_id"
				. " , sort_order"
				. " , status"
			. " ) VALUES ("
				. " '" . $this->name ."'"
				. " , '" . $this->category_id ."'"
				. " , '" . $this->sort_order ."'"
				. " , '" . $this->status ."'"
			. " )";
		return $this->sql_request($sql);
	}
	function db_update_name() {
		$sql = "update ".
			TABLE_PRODUCTS . " p" .
			" set p.name=". '"' . $this->name . '"'.
			" where p.id=" . $this->id;

		return $this->sql_request($sql);
	}
	function db_update_desc() {
		$sql = "update ".
			TABLE_PRODUCTS . " p" .
			" set p.description=". '"' . $this->description . '"'.
			" where p.id=" . $this->id;

		return $this->sql_request($sql);
	}
	function db_update() {
		$sql = "update ".
			TABLE_PRODUCTS . " p" .
			" set ";
		foreach ($this as $obKey => $obValue) {
			if ($obKey!="id" && $obValue) {
				$sql .= "p.". $obKey ."=". "'" . $obValue . "'";
				break;
			}
		}
		$sql .=" where p.id=" . $this->id;

		return $this->sql_request($sql);
	}
	 /*
	 * It saves the image file sent by the form 
	 */
	 function upload_picture($imageType) {
		global $server;
		if ($_FILES['Filedata']['name'] ) {
			$file = $_FILES['Filedata']['tmp_name'];
			$error = false;

			/**
			 * THESE ERROR CHECKS ARE JUST EXAMPLES HOW TO USE THE REPONSE HEADERS
			 * TO SEND THE STATUS OF THE UPLOAD, change them!
			 *
			 */

			if (!is_uploaded_file($file) || ($_FILES['Filedata']['size'] > 2 * 1024 * 1024) ){
				$error = '400 Bad Request';
			}
			if (!$error && !($size = @getimagesize($file))){
				$error = '409 Conflict';
			}
			if (!$error && !in_array($size[2], array(1, 2, 3, 7, 8) ) ){
				$error = '415 Unsupported Media Type';
			}
			if (!$error && ($size[0] < 25) || ($size[1] < 25)){
				$error = '417 Expectation Failed';
			}
			if ($error){
				/**
				 * ERROR DURING UPLOAD, one of the validators failed
				 * 
				 * see FancyUpload.js - onError for header handling
				 */
				 print "Error: $error";
				return(0);
			}
			else{
				/**
				 * UPLOAD SUCCESSFULL AND VALID
				 *
				 * Use move_uploaded_file here to save the uploaded file in your directory
				 */

			  	$extension=strrchr($_FILES['Filedata']['name'], '.');
			  	$strPath=$server->my_site[0]->path . $server->my_site[0]->folder . "catalog/images/" . $imageType;
			  	#Creamos el directorio si no existe
			  	is_dir($strPath) or exit("error");
			  	$unique_id = date('his');
			  	$filename = $strPath.'/'.$this->id . $extension;
		      	if (copy($file, $filename))
			  	{
				  $this->image=$this->id . $extension;
				  $this->db_update();
				  return true;
			  }
			}
		}
	 }
}

?>
